import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'package:pcp/common/config/config.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'mine_workstation_page.dart';

class AddReviewPage extends StatefulWidget {
  @override
  _AddReviewPageState createState() => new _AddReviewPageState();
}

class _AddReviewPageState extends State<AddReviewPage> {
  TextEditingController _nameController = new TextEditingController();
  TextEditingController _introController = new TextEditingController();
  TextEditingController _linkController = new TextEditingController();
  final List<Tab> tabs = <Tab>[
    new Tab(text: '任务',),
    new Tab(text: "收藏",)
  ];

  @override
  Widget build(BuildContext context) {
    final name = TextFormField(
      keyboardType: TextInputType.text,
      autofocus: true,
      controller: _nameController,
      decoration: InputDecoration(
        hintText: '添加任务名称',
        contentPadding: EdgeInsets.fromLTRB(10.0, 2.0, 20.0, 2.0),
      ),
      style: TextStyle(
        fontSize: 23,
      ),
    );

    final intro = TextFormField(
      keyboardType: TextInputType.text,
      autofocus: false,
      controller: _introController,
      decoration: InputDecoration(
        hintText: '写点什么来描述一下吧 ^_^',
        contentPadding: EdgeInsets.fromLTRB(10.0, 10.0, 20.0, 2.0),
      ),
      style: TextStyle(
        fontSize: 17,
      ),
    );

    final createButton = Padding(
      padding: EdgeInsets.symmetric(vertical: 16.0),
      child: Material(
        borderRadius: BorderRadius.circular(30.0),
        shadowColor: Colors.lightBlueAccent.shade100,
        elevation: 5.0,
        child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          onPressed: () async {
            SharedPreferences prefs = await SharedPreferences.getInstance();
            var userId = prefs.getString('userId');
            var projectId = prefs.getString('projectId');
            try {
              Response response = await Dio().post(
                  Config.BASE_URL + "/review/addReview",
                  data: {
                    "userId" : [userId],
                    "name" : _nameController.text,
                    "introduction" : _introController.text,
                    "projectId" : projectId
                  }
              );
              if (response.data['type'] == 1) {
                showToast("任务" + _nameController.text + "已创建");
                Navigator.of(context).push(new MaterialPageRoute(
                    builder: (BuildContext context) =>
                    new MineWorkStationPage()));
              } else {
                showToast("任务" + _nameController.text + "创建失败");
              }
            } catch (e) {
              print(e);
            }
          },
          color: Colors.blue,
          child: Text(
            '完成创建',
            style: TextStyle(color: Colors.white, fontSize: 20.0),
          ),
        ),
      ),
    );

    final link = TextFormField(
      keyboardType: TextInputType.text,
      autofocus: false,
      controller: _linkController,
      decoration: InputDecoration(
        hintText: '请输入分享链接',
        contentPadding: EdgeInsets.fromLTRB(10.0, 10.0, 20.0, 2.0),
      ),
      style: TextStyle(
        fontSize: 17,
      ),
    );

    final createButton2 = Padding(
      padding: EdgeInsets.symmetric(vertical: 16.0),
      child: Material(
        borderRadius: BorderRadius.circular(30.0),
        shadowColor: Colors.lightBlueAccent.shade100,
        elevation: 5.0,
        child: MaterialButton(
          minWidth: 200.0,
          height: 42.0,
          onPressed: () async {
            SharedPreferences prefs = await SharedPreferences.getInstance();
            var userId = prefs.getString('userId');
            var projectId = prefs.getString('projectId');
            try {
              Response response = await Dio().post(
                  Config.BASE_URL + "/collection/addCollection",
                  data: {
                    "userId" : userId,
                    "name" : _linkController.text,
                    "projectId" : projectId
                  }
              );
              if (response.data['type'] == 1) {
                showToast(_linkController.text + "已收藏");
                Navigator.of(context).push(new MaterialPageRoute(
                    builder: (BuildContext context) =>
                    new MineWorkStationPage()));
              } else {
                showToast(_linkController.text + response.data['info']);
              }
            } catch (e) {
              print(e);
            }
          },
          color: Colors.blue,
          child: Text(
            '完成收藏',
            style: TextStyle(color: Colors.white, fontSize: 20.0),
          ),
        ),
      ),
    );

    return new DefaultTabController(
      length: tabs.length,
      child: Scaffold(
        appBar: AppBar(
          elevation: 0,
          centerTitle: true,
          title: Text("新建任务/收藏"),
          bottom: new TabBar(
            tabs: tabs,
            // isScrollable: true,//这表示当标签栏内容超过屏幕宽度时是否滚动，因为我们有8个标签栏所以这里设置是
            labelColor: Colors.orange,//标签颜色
            unselectedLabelColor: Colors.white,//未被选中的标签的颜色
            labelStyle: new TextStyle(fontSize: 18.0),
          ),
        ),
        backgroundColor: Colors.white,
        body: TabBarView(
          children: [
            Center(
              child: new ListView(
                padding: const EdgeInsets.all(10.0),
                itemExtent: 80.0,
                children: <Widget>[
                  name,
                  intro,
                  createButton,
                ],
              ),
            ),
            Center(
              child: new ListView(
                padding: const EdgeInsets.all(10.0),
                itemExtent: 80.0,
                children: <Widget>[
                  link,
                  createButton2,
                ],
              ),
            ),
          ],
        )
      ),
    );
  }
}
